import pygame

# Define some colors
black	= (0, 0, 0)
white	= (255, 255, 255)
green	= (0, 255, 0)
red	= (255, 0, 0)
blue	= (0, 0, 255)
dkgreen	= (0, 100, 0)
purple	= (0xBF, 0x0F, 0xB5)
brown	= (0x55, 0x33, 0x00)
sandy_brown = (244, 164, 95)

def draw_item(screen, x, y):
	pygame.draw.rect(screen, purple, [x, y, 30, 10], 0)
	pygame.draw.circle(screen, black, [15 + x, 5 + y], 7, 0)

# Initialize the game engine
pygame.init()

# Set the height and width of the screen
width	= 640
height	= 480	
size 	= [width, height]
screen 	= pygame.display.set_mode(size)

pygame.display.set_caption("Title")

# Loop until the user clicks the close button
done = False
clock = pygame.time.Clock()

fps = 60

# Speed in pixels per frame
x_speed = 0
y_speed = 0

x_coord = 10
y_coord = 10

# ----- Main Program Loop -----
while not done:
	# For everything the user does
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			done = True

		# User pressed key
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_LEFT or event.key == pygame.K_a:
				x_speed = -3
			if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
				x_speed = 3
			if event.key == pygame.K_UP or event.key == pygame.K_w:
				y_speed = -3
			if event.key == pygame.K_DOWN or event.key == pygame.K_s:
				y_speed = 3

		# User released key
		if event.type == pygame.KEYUP:
			if event.key == pygame.K_LEFT or event.key == pygame.K_a:
				x_speed = 0
			if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
				x_speed = 0
			if event.key == pygame.K_UP or event.key == pygame.K_w:
				y_speed = 0
			if event.key == pygame.K_DOWN or event.key == pygame.K_s:
				y_speed = 0
			
	# Move the object according to speed
	x_coord += x_speed
	y_coord += y_speed

	# Check if it's outside the screen
	if x_coord > (width - 30):
		x_coord = width - 30
	if x_coord < 0:
		x_coord = 0
	if y_coord > (height - 10):
		y_coord = height - 10
	if y_coord < 0:
		y_coord = 0

	# Set the screen background
	screen.fill(sandy_brown)

	# Draw the item where the mouse is
	draw_item(screen, x_coord, y_coord)

	# Limit to fps frames per second
	clock.tick(fps)

	# Update the screen with what's been drawn
	pygame.display.flip()

# Exit.
pygame.quit()
